<?php
require $root . '/Queue/Queue.php';

/**
 * 带有尾指针的链表
 * Class LinkedListTail
 */
class QueueByLinkedList implements Queue
{
    private $head; //链表头部
    private $tail; //链表尾部
    private $size; //链表大小

    /**
     * 构造函数 初始化链表
     * QueueByLinkedList constructor.
     */
    public function __construct()
    {
        $this->head = null;
        $this->tail = null;
        $this->size = 0;
    }

    /**
     * 入队操作
     * @param $e
     */
    public function enqueue($e): void
    {
        if ($this->tail == null) {
            $this->tail = $this->head = new Node($e, null);
        } else {
            $node = new Node($e, null);
            $this->tail->next = $node;
            $this->tail = $node;
        }
        $this->size++;
    }


    /**
     * 出队操作
     * @return mixed
     */
    public function dequeue()
    {
        if ($this->size == 0) {
            return "队列已经是空的";
        }
        $node = $this->head;
        $this->head = $node->next;
        $this->size--;

        if ($node->next == null) {
            $this->tail = null;
        }
        return $node->e;
    }

    public function getFront()
    {
        if ($this->getSize() == 0) {
            echo "404";
            exit;
        }
        return $this->head->e;
    }

    public function getSize()
    {
        return $this->size;
    }

    /**
     * 判断队列是否为空
     * @return bool
     */
    public function isEmpty(): bool
    {
        return $this->size == 0;
    }

    public function toString()
    {
        $str = "";
        for ($node = $this->head; $node != null; $node = $node->next) {
            $str .= $node->e . "->";
        }
        $str .= "null";
        return $str;
    }
}

class Node
{
    public $e;//节点元素
    public $next; //下个节点信息

    /**
     * 构造函数 设置节点信息
     * Node constructor.
     * @param $e
     * @param $next
     */
    public function __construct($e, $next)
    {
        $this->e = $e;
        $this->next = $next;
    }
}